﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using System.Windows;
using ExpensesClient.ExpensesServiceReference;
using ExpensesClient.ExpensesStatusReference;
using Microsoft.Phone.Info;

namespace ExpensesClient.Repositories
{
    public class ExpenseService
    {
        private string _deviceUniqueId;

        public ExpenseService()
        {
        }

        public string DeviceUniqueId
        {
            get
            {
                if (_deviceUniqueId == null)
                {
                    var id = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
                    _deviceUniqueId = BitConverter.ToString(id);
                }
                return _deviceUniqueId;
            }
        }

        public void StartNew(Action onSuccess)
        {
            var proxy = new ServiceClient();
            proxy.StartNewCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    onSuccess();
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                Close(proxy);
            };
            proxy.StartNewAsync(DeviceUniqueId);
        }


        public void AddExpense(string description, decimal amount, Action<IList<ExpenseItem>> onSuccess)
        {
            var item = new ExpenseItem()
            {
                Description = description,
                Amount = amount
            };

            var proxy = new ServiceClient();
            proxy.AddExpenseCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    onSuccess(e.Result);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                Close(proxy);
            };

            proxy.AddExpenseAsync(DeviceUniqueId, item);
        }

        public void GetExpenses(Action<IList<ExpenseItem>> onSuccess)
        {
            var proxy = new ServiceClient();
            proxy.GetExpensesCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    onSuccess(e.Result);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                Close(proxy);
            };
            proxy.GetExpensesAsync(DeviceUniqueId);
        }

        public void OnSubmit(Action onSuccess)
        {
            var proxy = new ServiceClient();

            proxy.SubmitCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    onSuccess();
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                Close(proxy);
            };
            proxy.SubmitAsync(DeviceUniqueId);

        }

        public void GetSupprotedBookmarks(Action<string> onSuccess)
        {
            var proxy = new ExpensesStatusClient();
            proxy.GetActiveBookmarksCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    onSuccess(e.Result);
                }
                else
                {
                    MessageBox.Show(e.Error.Message);
                }

                Close(proxy);
            };

            proxy.GetActiveBookmarksAsync(DeviceUniqueId);
        }


        private void Close(ServiceClient proxy)
        {
            if (proxy.State != CommunicationState.Closed)
            {
                proxy.CloseCompleted += proxy_CloseCompleted;
                proxy.CloseAsync();
            }
        }

        private void Close(ExpensesStatusClient proxy)
        {
            if (proxy.State != CommunicationState.Closed)
            {
                proxy.CloseCompleted += proxy_CloseCompleted;
                proxy.CloseAsync();
            }
        }

        private void proxy_CloseCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                try
                {
                    var proxy = sender as ICommunicationObject;
                    proxy.Abort();
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
